package com.nginxmanager.comment;

import com.nginxmanager.domain.Conf;
import com.nginxmanager.domain.Model;

import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2018-01-11.
 */
public class ModelFileTool {
    //增加模块项
    public void addOne(String str,Model conf) {
        File file = new File(str+"\\conf\\nginx.conf");
        File fileOut = new File(str+"\\conf\\nginx123.conf");
        BufferedReader reader = null;
        BufferedWriter bw = null;
        String tempString = null;
        try {
            if (!fileOut.exists()) {
                fileOut.createNewFile();
            }
            FileWriter fw = new FileWriter(fileOut);
            bw = new BufferedWriter(fw);
            reader = new BufferedReader(new FileReader(file));
            int line = 1;
            while((tempString=reader.readLine()) != null){
                Pattern p = Pattern.compile("(.*)("+conf.getConsult()+")(.*)");
                Matcher m = p.matcher(tempString);
                bw.write(tempString + "\n");
                if (m.matches()){
                    bw.write(conf.getAllConf()+" {"+ "\n");
                    bw.write("     " + "\n");
                    bw.write("     }" + "\n");
                }
                line++;
            }
            reader.close();
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
            try {
                if (bw != null) {
                    bw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        file.delete();
        fileOut.renameTo(file);
    }

    //删除一项模块
    public void deleteOne(String str,Model conf) {
        File file = new File(str+"\\conf\\nginx.conf");
        File fileOut = new File(str+"\\conf\\nginx123.conf");
        BufferedReader reader = null;
        BufferedWriter bw = null;
        String tempString = null;
        try {
            if (!fileOut.exists()) {
                fileOut.createNewFile();
            }
            FileWriter fw = new FileWriter(fileOut);
            bw = new BufferedWriter(fw);
            reader = new BufferedReader(new FileReader(file));
            int line = 1;
            while((tempString=reader.readLine()) != null){
                Pattern p = Pattern.compile("(.*)("+conf.getKey()+")(.*)");
                Matcher m = p.matcher(tempString);
                if (m.matches()){
                    while((tempString=reader.readLine()).contains("}")) {
                        System.out.println("删除: " + conf.getAllConf());
                    }
                    continue;
                }
                bw.write(tempString + "\n");
                line++;
            }
            reader.close();
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
            try {
                if (bw != null) {
                    bw.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        file.delete();
        fileOut.renameTo(file);
    }
}
